﻿using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;
using System.Linq;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    public class CmsPageGetCountsDataCommand : CmsPageGetCountsCommand
    {
        public override CmsPageCounts ExecuteOne()
        {
            CmsPageCounts result = null;
            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsPageCounts>>();
            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsPageCounts>;
            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsPageCounts, int>>();
                var data = SqlUtility<CmsPageCounts, int>.ExecuteReader(
                    currentUnitOfWork.Connection,
                    currentUnitOfWork.Transaction,
                    "cms_sp_PageGetCountsForDifferentStatus",
                    new
                    {
                        SiteId = CommandParameter.SiteId,
                        SiteGroupId = CommandParameter.SiteGroupId,
                        PageTypeParameter = "Page",
                        PageId = CommandParameter.Id
                    },
                    mapper);
                result = data != null ? data.FirstOrDefault() : null;
            }
            return result;
        }
    }
}